<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\admin\model\goods\Order;
use app\common\controller\Api;
use app\common\controller\Payment;
use think\Controller;
use think\Db;
use think\Request;


class Dynamiccomments extends Api
{
    private static $model;
//    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';


    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        self::$model = new \app\common\model\DynamicComments();
    }

    /**
     * 列表（关于某条动态的所有评论）
     */
    public function lists()
    {
        $dynamicId = input('dynamic_id');
        if (!$dynamicId) {
            $this->error('参数缺失！');
        }

        $row_info = self::$model->find($dynamicId);
        if (!$row_info) {
            $this->error('该动态不存在！');
        }

        $pageSize = input('pagesize', 15);
        $lists = self::$model->where('dynamic_id', $dynamicId)->order('createtime', 'desc')->paginate($pageSize);

        $this->success('评论列表', $lists);
    }

    /**
     * 所有评论(type1为收到的，2为发出的)
     */
    public function listsAll()
    {
        $type = input('type', 1);
        if (!in_array($type, [1, 2])) {
            $this->error('参数非法！');
        }
        $pageSize = input('pagesize', 15);
        $userId = $this->auth->id;
        switch ($type) {
            case 1:
                $where = [
                    'status' => 1,
                    'user_id' => $userId
                ];

                $ids_arr = \app\common\model\UserDynamics::where($where)->column('id');
                $lists = self::$model->where('dynamic_id', 'in', $ids_arr)->order('createtime', 'desc')->paginate($pageSize);
                if ($lists) {
                    foreach ($lists as $val) {
                        $val->user;
                    }
                }
                break;
            case 2:
                $where = [
                    'status' => 1,
                    'user_id' => $userId
                ];

                $lists = self::$model->where($where)->order('createtime', 'desc')->paginate($pageSize);
                break;
        }

        $this->success('评论列表', $lists);
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function create()
    {
        $dynamicId = input('dynamic_id');
        if (!$dynamicId) {
            $this->error('参数缺失！');
        }

        $content = input('content');
        if (!$content) {
            $this->error('请输入评论！');
        }

        $cdata = [
            'user_id' => $this->auth->id,
            'dynamic_id' => $dynamicId,
            'content' => $content
        ];

        $res = self::$model->create($cdata);
        if (!$res) {
            $this->error('评论失败！');
        }

        \app\common\model\UserScoreLog::commentBonus($this->auth->id);
        $this->success('评论成功！');
    }
}
